<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*
** etc:  日志记录类
** author: deepe.xiao
** update: 2018-10-10
*/


class Log{
	protected $_log_path;
	protected $_enabled	= TRUE;
	
	public function __construct(){
		$this->_log_path = config_item('log_path');
		if ( ! is_dir($this->_log_path) )
		{
			$this->_enabled = FALSE;
		}
	}

	public function write_log($type,$msg){
		$filepath = $this->_log_path.'/'.$type."-".date(config_item('long_date_format')).'.php';
		$message  = '';
		if ( !file_exists($filepath) )
		{
			$message .= "<"."?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?".">\n\n";
		}

		if ( ! $fp = fopen($filepath, 'ab+'))
		{
			return FALSE;
		}

		$message .= $type."--".date('Y-m-d H:i:s'). ' --> '.$msg."\n";
		flock($fp, LOCK_EX);
		fwrite($fp, $message);
		flock($fp, LOCK_UN);
		fclose($fp);
		return TRUE;
	}
	
}
?>
